﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace PayloadTracker.Tiles
{
    public class ArcGisOnlineProvider : ITileProvider
    {
        public class AGOKey : ITileProvider.TileKey
        {
            public Coordinate UL, LR;
            public override string GetHash()
            {
                return UL.ToString() + LR.ToString();
            }
        }
        private string m_ID;
        private Uri m_URL;

        private ArcGisOnlineProvider()
        {

        }
        public ArcGisOnlineProvider(string ID, Uri URL)
        {
            m_ID = ID;
            m_URL = URL;
        }

        public override string Name()
        {
            return m_ID;
        }

        public override List<Tile> GetTilesForRegion(Coordinate UL, Coordinate LR, int Zoom, bool Async)
        {
            throw new NotImplementedException();
        }

        public override List<ITileProvider.TileKey> GetUniqueKeysForRegion(Coordinate UL, Coordinate LR, int Zoom)
        {
            List<ITileProvider.TileKey> ret = new List<TileKey>();

            var ulp = OSMProvider.Coordinate2Tile(UL, Zoom);
            var lrp = OSMProvider.Coordinate2Tile(LR, Zoom);

            for (int x = Math.Max(0, ulp.X - 1); x <= lrp.X; x++)
                for (int y = Math.Max(0, ulp.Y - 1); y <= lrp.Y; y++)
                {
                    var bb = OSMProvider.Tile2BoundingBox(x,y,Zoom);

                    AGOKey key = new AGOKey();
                    key.LR = bb.LR;
                    key.UL = bb.UL;
                    
                    ret.Add(key);

                }

            return ret;
        }

        public override Tile GetTileFromKey(ITileProvider.TileKey key, bool Async)
        {
            Tile tile;
            AGOKey ok = key as AGOKey;

            string tilepath = GetCachePath();
            string tilefile = Path.Combine(tilepath, ok.GetHash().Replace(".","-").Replace(":","")+".png");

            
            if (File.Exists(tilefile))
                tile = new Tile(System.Drawing.Image.FromFile(tilefile), ok.UL, ok.LR);
            else
            {
                var url = new System.Uri(m_URL + "/" + "export?bbox=" + ok.UL.Long.ToString() + "," + ok.UL.Lat.ToString() + "," + ok.LR.Long.ToString() + "," + ok.LR.Lat.ToString() + "&f=image&size=" + Tile.SIZE+ "," + Tile.SIZE);
                tile = new Tile(url, tilefile, ok.UL, ok.LR, Async);
            }

            return tile;
        }

        public override void ReadXml(System.Xml.XmlReader reader)
        {
            while (reader.MoveToNextAttribute())
            {
                switch (reader.Name.ToLower())
                {
                    case "id":
                        {
                            m_ID = reader.Value;
                            break;
                        }
                    case "enabled":
                        {
                            Enabled = reader.ReadContentAsBoolean();
                            break;
                        }
                    default:
                        {
                            Logger.LOG.Error("Unknown attribute specified in OSMProvider definition.");
                            break;
                        }
                }
            }

            reader.Read();

            string uristr = reader.ReadContentAsString();
            m_URL = new Uri(uristr);

            reader.Read();
            

        }

        public override void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteAttributeString("ID", m_ID);
            writer.WriteAttributeString("Enabled", Enabled.ToString());
            writer.WriteString(m_URL.ToString());

        }
    }
}
